package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.*;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/workspace")
public class WorkSpaceController {
    @Autowired
    private IOrdersService iOrdersService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IDishService iDishService;
    @Autowired
    private ISetmealService iSetmealService;
    @Autowired
    private IWorkspaceService iWorkspaceService;

    @GetMapping("/businessData")
    public Result businessData(){
        //因为导表要复用代码，所以提到Service层
        LocalDate date = LocalDate.now();
        LocalDateTime beginDate = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime endDate = LocalDateTime.of(date,LocalTime.MAX);

        BusinessDataVO vo=  iWorkspaceService.getBusinessData(beginDate,endDate);
        return Result.success(vo);
    }
    @GetMapping("/overviewOrders")
    public Result overviewOrders(){


        List<Orders> list = iOrdersService.list();
        //待接单数量
        long waitingOrders = list.stream().filter(s -> Objects.equals(s.getStatus(), Orders.TO_BE_CONFIRMED)).count();
        //待派送
        long deliveredOrders = list.stream().filter(s -> Objects.equals(s.getStatus(), Orders.CONFIRMED)).count();

        //完成订单
        long completedOrders = list.stream().filter(s -> Objects.equals(s.getStatus(), Orders.COMPLETED)).count();
        //取消的数量
        long cancelledOrders = list.stream().filter(s -> Objects.equals(s.getStatus(), Orders.CANCELLED)).count();

        //返回OrderOverViewVO

        OrderOverViewVO vo =OrderOverViewVO
                .builder()
                .waitingOrders((int) waitingOrders)
                .deliveredOrders((int) deliveredOrders)
                .completedOrders((int)completedOrders)
                .cancelledOrders((int) cancelledOrders)
                .allOrders(list.size())
                .build();

        return Result.success(vo);
    }

    @GetMapping("/overviewDishes")
    public Result overviewDishes(){
        List<Dish> list = iDishService.list();
        long sold = list.stream().filter(s -> s.getStatus().equals(StatusConstant.ENABLE)).count();
        long discontinued = list.stream().filter(s -> s.getStatus().equals(StatusConstant.DISABLE)).count();
        //DishOverViewVO
        DishOverViewVO vo =DishOverViewVO
                .builder()
                .sold((int) sold)
                .discontinued((int) discontinued)
                .build();
        return Result.success(vo);
    }
    @GetMapping("/overviewSetmeals")
    public Result overviewSetmeals(){
        List<Setmeal> list = iSetmealService.list();
        long sold = list.stream().filter(s -> s.getStatus().equals(StatusConstant.ENABLE)).count();
        long discontinued = list.stream().filter(s -> s.getStatus().equals(StatusConstant.DISABLE)).count();

        //SetmealOverViewVO
        SetmealOverViewVO vo =SetmealOverViewVO
                .builder()
                .sold((int) sold)
                .discontinued((int) discontinued)
                .build();
        return Result.success(vo);
    }
}
